package com.qingcheng.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.SkuMapper;
import com.qingcheng.pojo.goods.Sku;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class Init implements InitializingBean {

    @Autowired
    private SkuMapper skuMapper;

    @Override
    public void afterPropertiesSet() throws Exception {
        //1. 连接rest接口
        HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(httpHost);//rest构建器
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);//高级客户端对象
        //查询sku总数
        PageHelper.startPage(1, 5000);
        Page<Sku> skuPage = (Page<Sku>) skuMapper.selectAll();
        int pages = skuPage.getPages();
        for (int i = 1; i <= pages; i++) {
            PageHelper.startPage(i, 5000);
            List<Sku> skus = skuMapper.selectAll();
            BulkRequest bulkRequest = new BulkRequest();
            for (Sku sku : skus) {
                //封装请求对象
                IndexRequest indexRequest = new IndexRequest("sku", "doc", sku.getId());
                Map skuMap = new HashMap();
                skuMap.put("name", sku.getName());
                skuMap.put("price", sku.getPrice());
                skuMap.put("image", sku.getImage());
                Date createTime = sku.getCreateTime();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                if (createTime != null) {
                    String time = simpleDateFormat.format(createTime);
                    skuMap.put("createTime", time);
                }
                skuMap.put("spuId", sku.getSpuId());
                skuMap.put("categoryName", sku.getCategoryName());
                skuMap.put("brandName", sku.getBrandName());
                skuMap.put("commentNum", sku.getCommentNum());
                skuMap.put("saleNum", sku.getSaleNum());
                if (!StringUtils.isEmpty(sku.getSpec())){
                    Map<String, String> spec = (Map) JSON.parseObject(sku.getSpec());
                    skuMap.put("spec", spec);
                }
                indexRequest.source(skuMap);
                bulkRequest.add(indexRequest);
            }
            BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
            int status = bulkResponse.status().getStatus();
            System.out.println(status);
        }
        restHighLevelClient.close();
    }
}
